﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.Domain;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.DTOs.CourseTrainingAssign;
using ZenSystemService.Api.DTOs.Product;
using ZenSystemService.Api.DTOs.ProductType;
using ZenSystemService.Api.DTOs.TraineeCourseTraining;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    /// <summary>
    /// 分配学员到产品(设备)
    /// </summary>
    [Route("api/courseTrainingAssign")]
    [ApiController]
    public class CourseTrainingAssignController : ControllerBase
    {
        private readonly CourseTrainingAssignService _assignService;

        public CourseTrainingAssignController(CourseTrainingAssignService assignService)
        {
            _assignService = assignService;
        }

        /// <summary>
        /// 教练点击开始上课，分配学员到对应的客户端
        /// </summary>
        /// <returns></returns>
        [HttpPost("assignTraineeToProduct")]
        public async Task<IActionResult> AssignTraineeToProduct(AssignTraineeToProductRequestDTO req)
        {
            ReturnObjectT<IEnumerable<ProductTypeQueryResultDTO>> _returnObject = new ReturnObjectT<IEnumerable<ProductTypeQueryResultDTO>>();
            var userModel = User.ToUserModel();

            var assignResult = await _assignService.AssignTraineeToClient(userModel, req);
            return Ok(assignResult);
        }

        /// <summary>
        /// 教练点击开始上课，分配学员到对应的客户端
        /// </summary>
        /// <returns></returns>
        [HttpPost("assignTraineeToProducts")]
        public async Task<IActionResult> AssignTraineeToProducts(AssignTraineeToProductsRequestDTO req)
        {
            ReturnObjectT<IEnumerable<ProductTypeQueryResultDTO>> _returnObject = 
                new ReturnObjectT<IEnumerable<ProductTypeQueryResultDTO>>();
            var userModel = User.ToUserModel();

            var assignResult = await _assignService.AssignTraineeToClients(userModel, req);
            return Ok(assignResult);
        }

        /// <summary>
        /// 获取给当前客户端分配的用户是谁
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetAssignedUser")]
        public async Task<GetAssignedUserResponseDTO> GetAssignedUser(GetAssignedUserRequestDTO req)
        {
            //var productTypes = User.GetCurrentProducts();
            return await _assignService.GetAssignedUser(req);
        }

        /// <summary>
        /// 根据下发和使用状态获取产品列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetProductsByUsingStatus")]
        public async Task<ReturnObjectT<IEnumerable<GetProductsByUsingStatusResponseDTO>>> GetProductsByUsingStatus(ReceiveObjectT<GetProductsByUsingStatusRequestDTO> req)
        {
            return await _assignService.GetProductsByUsingStatusAsync(req);
        }

        /// <summary>
        /// 结束训练项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("RevokeAssignedUser")]
        public async Task<IActionResult> RevokeAssignedUser(RevokeAssignedUserRequestDTO req)
        {
            //var productTypes = User.GetCurrentProducts();
            await _assignService.RevokeAssignedUser(req);
            return Ok(new { Message = "Revoke assigned user successfully." });
        }
    }
}